package com.triptogether.trip.fragments;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.triptogether.R;
import com.triptogether.baseclasses.BaseFragment;
import com.triptogether.baseclasses.DateTimeDialog;
import com.triptogether.utils.db.DBManager;

public class CreateTripFragment extends BaseFragment implements OnDateSetListener {
	
	EditText editTextTripName;
	Button buttonCreateTrip;
	TextView textViewTripDate;
	Date chosenTripDate;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_create_trip, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);
	    
	    // Get the root View of this fragment
	    View rootView = getView();
	    
	    // Init the views in this fragments
		editTextTripName = (EditText) rootView.findViewById(R.id.edittext_trip_name);
		buttonCreateTrip = (Button) rootView.findViewById(R.id.button_create_trip);
		textViewTripDate = (TextView) rootView.findViewById(R.id.textview_trip_date_value);
		
		// Init the chosen trip date views
		chosenTripDate = Calendar.getInstance().getTime();
		textViewTripDate.setText(SimpleDateFormat.getDateInstance().format(chosenTripDate));
		textViewTripDate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DateTimeDialog dateTimeDialog = new DateTimeDialog(
				        getString(R.string.choose_trip_date_description),
				        CreateTripFragment.this);
				
				dateTimeDialog.show(getFragmentManager(), "dateTimeDialog");
			}
		});
		
		// Add listeners to the views
		editTextTripName.addTextChangedListener(editTextsWatcher);
		buttonCreateTrip.setOnClickListener(createTripListener);
	}
	
	TextWatcher editTextsWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if(s.toString().trim().length() > 0) {
				buttonCreateTrip.setEnabled(true);
			} else {
				buttonCreateTrip.setEnabled(false);
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
		
		@Override
		public void afterTextChanged(Editable s) {}
	};
	
	OnClickListener createTripListener = new OnClickListener() {		
		@Override
		public void onClick(View v) {
			// Create Trip in the DB
			DBManager.getInstance(getActivity()).createTrip(
			        editTextTripName.getText().toString(), chosenTripDate);

			chosenTripDate = Calendar.getInstance().getTime();
			textViewTripDate.setText(SimpleDateFormat.getDateInstance().format(chosenTripDate));
			editTextTripName.setText("");
			buttonCreateTrip.setEnabled(false);
		}
	};

	@Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, monthOfYear, dayOfMonth);
		chosenTripDate = calendar.getTime();
		
		// Set the text on the
		textViewTripDate.setText(SimpleDateFormat.getDateInstance().format(chosenTripDate));
    }
}
